package cn.chencq.thread;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TraditionalTimer {
	private static int count = 0;

	public static void main(String[] args) {
		test3();

		while (true) {
			System.out.println(new Date().getSeconds());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * new Timer().schedule(TimerTask,delay,peroid)
	 * 
	 * delay 
	 * 
	 * period    
	 */
	public static void test1() {
		new Timer().schedule(new TimerTask() {

			@Override
			public void run() {
				System.out.println("blombing!");
			}
		}, 10000, 3000);
	}

	/**
	 * new Timer().schedule(TimerTask,Date,peroid)
	 * 
	 * Date 
	 * 
	 * period 
	 */
	public static void test2() {
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				System.out.println("blombing!");
			}
		}, new Date(116, 3, 22, 19, 20, 59), 3000);
	}

	/**
	 * new Timer().scheduleAtFixedRate(TimerTask,,Date,peroid)
	 *  
	 * Date 
	 * 
	 * period 
	 */
	public static void test3() {
		new Timer().scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				System.out.println("blombing!");
			}
		}, new Date(116, 3, 22, 19, 20, 59), 3000);
	}

	public static void test5() {
		class MyTimerTask extends TimerTask {
			@Override
			public void run() {
				count = (count + 1) % 2;
				System.out.println("bombing!");
				new Timer().schedule(new MyTimerTask(), 2000 * count + 2000);
			}

		}

		new Timer().schedule(new MyTimerTask(), 2000);
	}
}
